﻿using Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DormMS.IService;
using DormMS.Model;
using DormMS.Model.ViewModels;
using SqlSugar;
using Infrastructure.Tool;
using DormMS.Model.Enum;
using ServiceStack;

namespace WeChat.Controllers
{
    public class OrderController : BaseController
    {
        private ICouponService _couponService;
        private ICommonAreaService _areaService;
        private IProductService _proService;
        private IOrderService _orderService;
        private IProductCategoryService _proCateService;
        private IFreightService _freightService;
        private IExchangeService _exchangeService;
        private ISpikeService _spikeService;
        private IDropGroupService _dropService;
        private IActivityService _actService;
        private IActivityTeamMembersService _activityteammembersservice;
        private IGroupHeadService _groupheadservice;
        public OrderController(IMemberService s, IExchangeService exchangeService, ICouponService couponService, ICommonAreaService areaService, IProductService proService, IOrderService orderService, IProductCategoryService proCateService, IFreightService freightService, ISpikeService spikeService, IDropGroupService dropService, IActivityService actService, IActivityTeamMembersService activityteammembersservice, IGroupHeadService groupheadservice) : base(s)
        {
            _couponService = couponService;
            _areaService = areaService;
            _proService = proService;
            _orderService = orderService;
            _proCateService = proCateService;
            _freightService = freightService;
            _exchangeService = exchangeService;
            _spikeService = spikeService;
            _dropService = dropService;
            _actService = actService;
            _activityteammembersservice = activityteammembersservice;
            _groupheadservice = groupheadservice;
        }

        public ActionResult OrderDetail()
        {
            return View();
        }

        public JsonResult GetOrderInfo(int id)
        {
            Result result = new Result();
            try
            {
                var model = _orderService.GetOrderModel(id);
                model.V_OrderDetailList = _orderService.GetV_OrderDetailByOrderId(id);
                model.DeliveryList = _orderService.GetDeliveryListByOrderId(id);

                result.data = model;
                if (model.OrderType == OrderType.一分钱订单)
                {
                    var model1 = _actService.GetActivityInfo(model.AactivityId.Value);
                    var model2 = _activityteammembersservice.GetActivityTeamMembersInfo(model.OrderNo);
                    result.data1 = model1;
                    result.data2 = model2;
                }
                else if (model.OrderType == OrderType.团购订单)
                {
                    var model1 = _dropService.GetDropGroupInfo(model.AactivityId.Value);
                    var model2 = _groupheadservice.GetGroupMemberInfo(model.OrderNo);
                    result.data1 = model1;
                    result.data2 = model2;
                }
                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult OrderSubmit()
        {
            return View();
        }

        public JsonResult GetAddressList()
        {
            Result result = new Result();
            try
            {
                result.data = _memberService.GetV_MemberAdressList(CurrentLoginUser.Id);
                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }



        public JsonResult GetProductList(string ids, int areaId)
        {
            Result result = new Result();
            try
            {
                if (!string.IsNullOrWhiteSpace(ids))
                {
                    ids = ids.Substring(0, ids.Length - 1);
                    var sclist = _proService.GetShopCartList(ids);
                    var member = _memberService.GetMemberInfo(CurrentLoginUser.Id);
                    var level = _memberService.GetMemberLevelInfo(member.MemberLevel);
                    Order order = new Order();
                    order.OrderType = DormMS.Model.Enum.OrderType.正常订单;
                    order.ProductPrice = 0;
                    decimal pointPrice = 0;
                    int usePoint = 0;
                    foreach (var item in sclist)
                    {

                        if (level.Discount > 0 && order.OrderType == OrderType.正常订单)
                        {
                            order.ProductPrice += decimal.Parse((item.Price * item.Num * (level.Discount * (decimal)0.01)).ToString("0.00"));
                        }
                        else
                        {
                            order.ProductPrice += (item.Price * item.Num);
                        }
                        if (item.IsAllowUsePoints)
                        {
                            pointPrice += (item.Price * item.Num * (level.Discount * (decimal)0.01) * item.UsePointRate * (decimal)0.01);
                        }
                    }
                    if (pointPrice > 0)
                    {
                        pointPrice = decimal.Parse(pointPrice.ToString("0.00"));
                        if (member.Points < pointPrice * 100)
                        {
                            usePoint = member.Points;
                        }
                        else
                        {
                            usePoint = int.Parse((pointPrice * 100).ToString("0"));
                        }
                    }

                    var pcids = sclist.Select(t => t.CategoryId).ToArray();
                    var cids = _proCateService.GetProductCategoryId(pcids);
                    string cateids = string.Join(",", cids);

                    result.data1 = _couponService.GetMyCouponList(CurrentLoginUser.Id, order.ProductPrice, cateids);

                    decimal postmoney = 0;
                    if (areaId > 0)
                    {
                        var r = _freightService.GetPostageMoney(sclist, areaId, out postmoney);
                    }
                    order.PostageMoney = postmoney;
                    result.data2 = usePoint / (decimal)100;
                    result.data = sclist;
                    result.model = order;
                    result.isSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetOrderProductList(int id, int num, int areaId, int actId = 0, int actType = 0, int groupId = 0)
        {
            Result result = new Result();
            try
            {
                Order order = new Order();
                if (actId == 0)
                {
                    order.OrderType = DormMS.Model.Enum.OrderType.正常订单;
                }
                else
                {
                    order.OrderType = (DormMS.Model.Enum.OrderType)actType;
                    order.AactivityId = actId;

                }
                decimal actprice = 0;
                if (order.OrderType == OrderType.团购订单)
                {
                    var act = _dropService.GetDropGroupInfo(actId);
                    if (act == null)
                    {
                        result.msg = "活动不存在或已经下架";
                        return Json(result, JsonRequestBehavior.AllowGet);
                    }
                    id = act.SpecificationsID;
                    actprice = act.StartingPrice;
                }

                var pro = _proService.GetProduct_SpecInfo(id);

                if (pro == null)
                {
                    result.msg = "商品不存在或已经下架";
                    return Json(result, JsonRequestBehavior.AllowGet);
                }




                List<CartProduct> plist = new List<CartProduct>();
                CartProduct item = new CartProduct();
                item.SpecId = id;
                item.Num = num;
                item.Price = pro.Price;
                if (order.OrderType == OrderType.抢购订单)
                {
                    var actPro = _spikeService.GetSpikeProduct(actId, pro.ProductSku);
                    item.Price = actPro.Price;
                }
                else if (order.OrderType == OrderType.团购订单)
                {
                    item.Price = actprice;
                    if (groupId > 0)
                    {
                        order.GroupId = groupId;

                        var group = _dropService.FindGroupHead(groupId);
                        if (group == null)
                        {
                            result.msg = "团不存在";
                            return Json(result, JsonRequestBehavior.AllowGet);
                        }
                        item.Price = group.NowPrice;
                    }
                }

                item.ProductName = pro.Name;
                item.ProductSku = pro.ProductSku;
                item.ProductId = pro.ProductId;
                item.ProductWeight = pro.ProductWeight;
                item.SpecNames = pro.SpecNames;
                item.UsePointRate = pro.UsePointRate;
                item.IsAllowUseCoupon = pro.IsAllowUseCoupon;
                item.IsAllowUsePoints = pro.IsAllowUsePoints;
                item.ImgUrl = pro.ImgUrl;
                item.FreightTemplateId = pro.FreightTemplateId;
                item.CategoryId = pro.CategoryId;
                item.UniformFreight = pro.UniformFreight;
                item.CostPrice = pro.CostPrice;
                plist.Add(item);
                var member = _memberService.GetMemberInfo(CurrentLoginUser.Id);
                var level = _memberService.GetMemberLevelInfo(member.MemberLevel);

                if (level.Discount > 0 && order.OrderType == OrderType.正常订单)
                {
                    order.ProductPrice = item.Price * item.Num * (level.Discount * (decimal)0.01);
                }
                else
                {
                    order.ProductPrice = item.Price * item.Num;
                }

                var pcids = plist.Select(t => t.CategoryId).ToArray();
                var cids = _proCateService.GetProductCategoryId(pcids);
                string cateids = "";
                if (cids.Length == 1)
                {
                    cateids = string.Join(",", cids);
                }
                decimal pointPrice = 0;
                int usePoint = 0;
                if (item.IsAllowUsePoints)
                {
                    pointPrice += (order.ProductPrice * item.UsePointRate * (decimal)0.01);
                }
                if (pointPrice > 0)
                {
                    pointPrice = decimal.Parse(pointPrice.ToString("0.00"));
                    if (member.Points < pointPrice * 100)
                    {
                        usePoint = member.Points;
                    }
                    else
                    {
                        usePoint = int.Parse((pointPrice * 100).ToString("0"));
                    }
                }

                decimal postmoney = 0;
                if (areaId > 0)
                {
                    var r = _freightService.GetPostageMoney(plist, areaId, out postmoney);
                }
                order.PostageMoney = postmoney;


                result.data1 = _couponService.GetMyCouponList(CurrentLoginUser.Id, order.ProductPrice, cateids);

                result.data2 = usePoint / (decimal)100;
                result.data = plist;
                result.model = order;
                result.isSuccess = true;

            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetPostageMoney(List<CartProduct> plist, int areaId)
        {
            Result result = new Result();
            try
            {
                decimal postmoney = 0;
                _freightService.GetPostageMoney(plist, areaId, out postmoney);
                result.data = postmoney;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 提交订单
        /// </summary>
        /// <param name="order"></param>
        /// <param name="plist"></param>
        /// <param name="couponId"></param>
        /// <returns></returns>
        public JsonResult SubmitOrder(Order order, List<CartProduct> plist, bool isUseBalance, int? couponId)
        {
            Result result = new Result();
            try
            {
                foreach (var item in plist)
                {
                    if (!_proService.IsCanBuy(order.ReceiveCity, item.ProductId))
                    {
                        result.msg = item.ProductName+"超出配送范围,无法购买";
                        return Json(result, JsonRequestBehavior.AllowGet);
                    }
                }

                var member = _memberService.GetMemberInfo(CurrentLoginUser.Id);
                var level = _memberService.GetMemberLevelInfo(member.MemberLevel);
                decimal pointPrice = 0;
                List<Member_ShopCart> cartlist = new List<Member_ShopCart>();
                order.MemberId = CurrentLoginUser.Id;
                order.MemberName = member.NickName;
                order.MemberDiscount = level.Discount;
                order.MemberLevel = member.MemberLevel;
                order.ProductPrice = 0;
                order.OrderPoint = 0;
                order.ActivityCouponMoney = 0;
                order.PointsPayMoney = 0;
                order.PayType = PayType.微信支付;
                decimal postmoney = 0;
                var r = _freightService.GetPostageMoney(plist, order.ReceiveProvince, out postmoney);
                if (r.isSuccess)
                {
                    order.PostageMoney = postmoney;
                }
                else
                {
                    return Json(r, JsonRequestBehavior.AllowGet);
                }
                if (order.OrderType == DormMS.Model.Enum.OrderType.正常订单)
                {
                    order.OrderNo = _orderService.GetSerialNumber("W");
                }
                else if (order.OrderType == DormMS.Model.Enum.OrderType.一分钱订单)
                {
                    order.OrderNo = _orderService.GetSerialNumber("Y");
                    order.PostageMoney = 0;
                }
                else if (order.OrderType == DormMS.Model.Enum.OrderType.抢购订单)
                {
                    order.OrderNo = _orderService.GetSerialNumber("X");
                }
                else if (order.OrderType == DormMS.Model.Enum.OrderType.团购订单)
                {
                    order.OrderNo = _orderService.GetSerialNumber("T");
                }


                List<OrderDetail> odlist = new List<OrderDetail>();
                foreach (CartProduct item in plist)
                {
                    var p = _proService.GetProduct_SpecInfo(item.SpecId);

                    if (p == null)
                    {
                        result.msg = item.ProductName + "或已下架";
                        return Json(result, JsonRequestBehavior.AllowGet);
                    }

                    if (item.Num > p.Stock - p.WaitSendStock)
                    {
                        result.msg = p.Name + "库存不足";
                        return Json(result, JsonRequestBehavior.AllowGet);
                    }
                    if (order.OrderType == OrderType.抢购订单)
                    {
                        var splike = _spikeService.GetSpikeProduct(order.AactivityId.Value, item.ProductSku);
                        if (splike == null)
                        {
                            result.msg = "抢购商品不存在";
                            return Json(result, JsonRequestBehavior.AllowGet);
                        }

                        int mycount = _orderService.GetMemberSpikeCount(CurrentLoginUser.Id, item.ProductId);
                        if (splike.Limit < mycount + item.Num)
                        {
                            result.msg = "每人限购" + splike.Limit;
                            return Json(result, JsonRequestBehavior.AllowGet);
                        }
                        if (splike.Stock - splike.SpikeStock - item.Num < 0)
                        {
                            result.msg = " 库存不足";
                            return Json(result, JsonRequestBehavior.AllowGet);
                        }
                        p.Price = splike.Price;
                    }
                    else if (order.OrderType == OrderType.团购订单)
                    {
                        var act = _dropService.GetDropGroupInfo(order.AactivityId.Value);
                        if (act == null || act.State != 0)
                        {
                            result.msg = "活动不存在或已下架";
                            return Json(result, JsonRequestBehavior.AllowGet);
                        }
                        if (act.EndTime <= DateTime.Now)
                        {
                            result.msg = "活动已结束";
                            return Json(result, JsonRequestBehavior.AllowGet);
                        }
                        p.Price = act.StartingPrice;
                        if (order.GroupId.HasValue && order.GroupId.Value > 0)
                        {
                            var group = _dropService.FindGroupHead(order.GroupId.Value);
                            if (group == null)
                            {
                                result.msg = "团不存在";
                                return Json(result, JsonRequestBehavior.AllowGet);
                            }
                            p.Price = group.NowPrice;
                        }
                    }


                    OrderDetail od = new OrderDetail();
                    od.SpecId = item.Id;
                    od.ProductCount = item.Num;
                    od.ProductPrice = p.Price;
                    od.ImgUrl = p.ImgUrl;
                    od.ProductName = item.ProductName;
                    od.ProductSku = p.ProductSku;
                    od.OrderNo = order.OrderNo;
                    od.MarketPrice = p.MarketPrice;
                    od.CostPrice = p.CostPrice;
                    od.SpecId = p.Id;
                    od.SpecKey = p.SpecIds;
                    od.SpecName = p.SpecNames;
                    od.SendStatus = DormMS.Model.Enum.OrderDetailSendStatus.未发货;
                    od.Discount = level.Discount;
                    if (level.Discount > 0 && order.OrderType == OrderType.正常订单)
                    {
                        od.DiscountPrice = decimal.Parse((od.ProductPrice * level.Discount * (decimal)0.01).ToString("0.00"));
                    }
                    else
                    {
                        od.DiscountPrice = od.ProductPrice;
                    }

                    if (item.IsAllowUsePoints)
                    {
                        pointPrice += (od.DiscountPrice * item.Num * item.UsePointRate * (decimal)0.01);
                    }
                    od.Points = (od.DiscountPrice * (level.OrderPoint * (decimal)0.01)).ToString("0").ObjToInt();
                    odlist.Add(od);

                    order.ProductPrice += od.DiscountPrice * od.ProductCount;
                    order.OrderPoint += od.Points * od.ProductCount;

                    if (item.Id > 0)
                    {
                        Member_ShopCart ms = _proService.GetMember_ShopCart(item.Id);
                        if (ms != null)
                        {
                            cartlist.Add(ms);
                        }
                    }
                }
                //优惠券逻辑
                if (couponId.HasValue && couponId > 0)
                {
                    var coupon = _couponService.GetMemberCouponInfoById(couponId.Value);
                    if (coupon.Consumption > order.ProductPrice)
                    {
                        result.msg = "优惠券需要满" + coupon.Consumption + "元才能使用";
                        return Json(result, JsonRequestBehavior.AllowGet);
                    }
                    if (order.UsePoints > 0 && !coupon.IsUsePoint)
                    {
                        result.msg = "使用此优惠券不可同时使用积分";
                        return Json(result, JsonRequestBehavior.AllowGet);
                    }

                    order.CouponId = couponId.Value;
                    order.ActivityCouponMoney = coupon.Amount;
                }
                //使用积分
                if (member.Points > 0 && order.UsePoints > 0 && pointPrice > 0)
                {
                    decimal need = order.ProductPrice + order.PostageMoney - order.ActivityCouponMoney;

                    pointPrice = decimal.Parse(pointPrice.ToString("0.00"));
                    if (member.Points < pointPrice * 100)
                    {
                        order.UsePoints = member.Points;
                    }
                    else
                    {
                        order.UsePoints = int.Parse((pointPrice * 100).ToString("0"));
                    }

                    order.PointsPayMoney = order.UsePoints * (decimal)0.01;

                    if (order.PointsPayMoney >= need)
                    {
                        order.PointsPayMoney = need;
                        order.UsePoints = int.Parse((need * 100).ToString("0"));
                    }

                }
                order.OrderPrice = order.ProductPrice + order.PostageMoney;
                //是否使用余额
                if (isUseBalance && order.ProductPrice + order.PostageMoney - order.PointsPayMoney - order.ActivityCouponMoney > 0)
                {
                    if (member.Balance <= 0)
                    {
                        result.msg = "余额不足";
                        return Json(result, JsonRequestBehavior.AllowGet);
                    }
                    decimal need = order.ProductPrice + order.PostageMoney - order.PointsPayMoney - order.ActivityCouponMoney;
                    if (member.Balance >= need)
                    {
                        order.MemberMoney = need;
                    }
                    else
                    {
                        order.MemberMoney = member.Balance;
                    }
                }

                //支付金额
                order.PayMoney = order.ProductPrice + order.PostageMoney - order.PointsPayMoney - order.MemberMoney - order.ActivityCouponMoney;
                if (order.PayMoney <= 0)
                {
                    order.PayMoney = 0;
                    order.PayState = DormMS.Model.Enum.OrderPayStatus.已支付;
                    result.url = "shop.success";
                    if (order.OrderType == OrderType.团购订单)
                    {
                        result.url = "shop.actsuccess";
                    }
                }
                else
                {
                    result.url = "shop.orderpay";
                }

                OrderRecord or = new OrderRecord();
                or.OperationUserType = 0;
                or.OperationId = CurrentLoginUser.Id;
                or.ClientName = "微信";

                result.data = _orderService.AddOrder(order, odlist, or);
                if (cartlist != null && cartlist.Count > 0)
                {
                    _proService.DelCart(cartlist);
                }
                result.isSuccess = true;

            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }


        public JsonResult CancelOrder(int id)
        {
            Result result = new Result();
            try
            {
                OrderRecord or = new OrderRecord();
                or.OperationId = CurrentLoginUser.Id;
                or.ClientName = "微信";
                or.Remark = "用户取消订单";
                or.OperationUserType = 0;
                or.Ip = Generator.GetIP();
                _orderService.CancelOrder(id, or);
                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }
            return Json(result);
        }

        //删除订单
        public JsonResult DelOrder(int id)
        {
            Result result = new Result();
            try
            {
                OrderRecord or = new OrderRecord();
                or.OperationId = CurrentLoginUser.Id;
                or.ClientName = "前台";
                or.OperationUserType = 0;
                or.Remark = "用户删除订单";

                _orderService.DelOrder(id, or);
                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;

            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }


        //确认收货
        public JsonResult ConfirmOrder(int id, string note)
        {
            Result result = new Result();
            try
            {


                var order = _orderService.GetOrderInfo(id);
                if (order != null)
                {

                    order.OrderState = OrderStatus.已收货;
                    order.ReceiveTime = DateTime.Now;
                    _orderService.UpdateOrder(order);
                    OrderRecord or = new OrderRecord();
                    or.OperationId = CurrentLoginUser.Id;
                    or.ClientName = "前台";
                    or.OperationUserType = 1;
                    or.Remark = note;
                    or.OrderId = order.Id;
                    or.OrderNo = order.OrderNo;
                    or.PayStatus = order.PayState;
                    or.SendStatus = order.SendStatus;
                    or.OrderStatus = order.OrderState;
                    or.OperationNote = "确认收货";
                    _orderService.AddOrderRecord(or);
                    result.isSuccess = true;


                }
                else
                {
                    result.msg = "订单不存在";
                }

            }
            catch (Exception ex)
            {
                result.msg = ex.Message;

            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Success()
        {
            return View();
        }

        public ActionResult ActSuccess()
        {
            return View();
        }

        #region 积分订单

        public ActionResult PointOrder()
        {
            return View();
        }

        public JsonResult GetPointProduct(int id)
        {
            Result result = new Result();
            try
            {
                result.data = _exchangeService.FindProductExchange(id);
                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;

            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult SubmitPointOrder(Order order, int productId)
        {
            Result result = new Result();
            try
            {
                var member = _memberService.GetMemberInfo(CurrentLoginUser.Id);
                var product = _exchangeService.FindProductExchange(productId);
                var level = _memberService.GetMemberLevelInfo(product.ExchangeLevel);
                if (product.ExchangeLevel > member.MemberLevel)
                {
                    result.msg = "会员等级不够，需" + level.LevelName + "才能兑换";
                }
                else if (member.Points < product.Points)
                {
                    result.msg = "积分不足";
                }
                else
                {
                    List<OrderDetail> odlist = new List<OrderDetail>();

                    OrderDetail od = new OrderDetail();
                    od.ProductCount = 1;
                    od.Points = product.Points;
                    od.ProductId = product.Id;
                    od.ImgUrl = product.ImgUrl;
                    od.MarketPrice = product.MarketPrice;
                    od.ProductDesc = product.Description;
                    od.ProductName = product.Name;

                    odlist.Add(od);

                    order.OrderNo = _orderService.GetSerialNumber("P");
                    order.OrderType = OrderType.积分订单;
                    order.UsePoints = product.Points;
                    order.MemberId = CurrentLoginUser.Id;
                    order.PayState = OrderPayStatus.已支付;
                    order.PayTime = DateTime.Now;
                    order.SendStatus = OrderSendStatus.未发货;
                    order.OrderPoint = 0;
                    order.OrderState = OrderStatus.待确认;
                    OrderRecord or = new OrderRecord();
                    or.OperationUserType = 0;
                    or.OperationId = CurrentLoginUser.Id;
                    or.ClientName = "微信";

                    _orderService.AddOrder(order, odlist, or);

                    result.isSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;

            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetMyPointOrderList()
        {
            Result result = new Result();
            try
            {
                result.data = _orderService.GetMyPointOrderList(CurrentLoginUser.Id);
                result.isSuccess = true;
            }
            catch (Exception ex)
            {

                result.msg = ex.Message;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }


        #endregion

        #region 退货
        public ActionResult ReturnGoods()
        {
            return View();
        }

        public JsonResult SaveReturnGoods(V_ReturnGoods model, List<string> imgList)
        {
            Result result = new Result();
            try
            {
                var orderDetail = _orderService.FindOrderDetail(model.OrderDetailId);
                var order = _orderService.GetOrderInfo(orderDetail.OrderId);
                if (order != null)
                {

                    DormMS.Model.ReturnGoods returnGood = new DormMS.Model.ReturnGoods();
                    returnGood.ReturnNo = _orderService.GetSerialNumber("D");
                    returnGood.OrderNo = order.OrderNo;
                    returnGood.OrderId = order.Id;
                    returnGood.OrderDetailId = model.OrderDetailId;
                    returnGood.Reason = model.Reason;
                    returnGood.ReturnAmount = 0;
                    returnGood.ReturnType = model.ReturnType;
                    returnGood.Satus = ReturnStatus.待处理;
                    returnGood.Imgs = (imgList ?? new List<string>()).ToJson();
                    returnGood.MemberId = CurrentLoginUser.Id;
                    _orderService.AddReturnGoods(returnGood);
                    result.isSuccess = true;
                }
                else
                {
                    result.msg = "订单不存在";
                }

            }
            catch (Exception ex)
            {
                result.msg = ex.Message;

            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        #endregion

        /// <summary>
        /// 收货
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GoodsReceipt(int id)
        {
            Result result = new Result();
            var success = _orderService.GoodsReceipt(id,CurrentLoginUser.Id);

            if (success == 1)
            {
            
                result.isSuccess = true;
                result.msg = "操作成功";
            }
            else
            {
                result.isSuccess = false;
                result.msg = "操作失败";
            }
            return Json(result);
        }

        #region 一分钱活动

        public ActionResult ActOrder()
        {
            return View();
        }

        public JsonResult GetActInfo(int actId = 0, int groupId = 0)
        {
            Result result = new Result();
            try
            {
                Order order = new Order();
                order.OrderType = OrderType.一分钱订单;
                order.AactivityId = actId;
                var act = _actService.GetActivityInfo(actId);


                if (act == null || act.EndTime.Date < DateTime.Today)
                {
                    result.msg = "活动不存在或已经下架";
                    return Json(result, JsonRequestBehavior.AllowGet);
                }

                if (groupId > 0)
                {
                    order.GroupId = groupId;
                }

                order.ProductPrice = act.ActivityPrice;
                order.PayMoney = act.ActivityPrice;
                order.OrderPrice = act.ActivityPrice;




                var member = _memberService.GetMemberInfo(CurrentLoginUser.Id);
                order.MemberId = member.Id;
                order.MemberName = member.NickName;

                result.model = order;
                result.isSuccess = true;

            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult SubmitActOrder(Order order)
        {
            Result result = new Result();
            try
            {
                var member = _memberService.GetMemberInfo(CurrentLoginUser.Id);
                var act = _actService.GetActivityInfo(order.AactivityId.Value);



                if (act == null || act.EndTime.Date < DateTime.Today || act.ActivityStatus == 1 || act.StartTime.Date > DateTime.Today)
                {
                    result.msg = "活动还未开始或已下架";
                }
                else
                {

                    if (order.GroupId.HasValue && order.GroupId.Value > 0)
                    {
                        if (_actService.IsCanJoin(CurrentLoginUser.Id, order.AactivityId.Value, order.GroupId.Value))
                        {

                        }
                    }
                    else if (_actService.IsCanCreat(CurrentLoginUser.Id, order.AactivityId.Value))
                    {

                    }
                    List<OrderDetail> odlist = new List<OrderDetail>();



                    order.OrderNo = _orderService.GetSerialNumber("Y");
                    order.OrderType = OrderType.一分钱订单;
                    order.UsePoints = 0;
                    order.MemberId = CurrentLoginUser.Id;
                    order.PayState = OrderPayStatus.未支付;
                    order.SendStatus = OrderSendStatus.未发货;
                    order.OrderPoint = 0;
                    order.OrderState = OrderStatus.待确认;
                    order.PayMoney = act.ActivityPrice;
                    order.ProductPrice = act.ActivityPrice;
                    OrderRecord or = new OrderRecord();
                    or.OperationUserType = 0;
                    or.OperationId = CurrentLoginUser.Id;
                    or.ClientName = "微信";

                    result.data = _orderService.AddOrder(order, odlist, or);

                    result.isSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;

            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}